<?php

namespace app\command;

use Swoole\Process;
use think\swoole\command\Server;
use think\console\Input;
use think\console\Output;
use think\swoole\Manager;


class CustomSwooleCommand extends Server
{
    public function handle(Manager $manager)
    {
        //添加定时器
        $this->addTimer($manager);

        parent::handle($manager);
    }

    protected function execute(Input $input, Output $output)
    {
        // 调用父类的 execute 方法，运行原始 Swoole 命令逻辑
        parent::execute($input, $output);
    }

    public function addTimer(Manager $manager)
    {
        if ($manager->getConfig('timer.enable', false)) {
            $handler = $manager->getConfig('timer.handler', '');
            if (!$handler) {
                $this->output->writeln("<error>Timer 进程启动失败</error>，请在<info>config/swoole.php</info>文件的<info>timer.handler</info>项指定驱动类");
            } else {
                $manager->addWorker(function (Process\Pool $pool) use ($manager, $handler) {
                    new $handler($manager, $pool);
                }, 'timer');
            }
        }

    }
}
